package com.senlan.water.controller.sms;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senlan.water.common.result.BaseResult;
import com.senlan.water.dao.domain.ShortMessageTemplate;
import com.senlan.water.dao.service.ShortMessageTemplateService;
import com.senlan.water.dto.sms.ShortMessageTemplateAddDTO;
import com.senlan.water.dto.sms.ShortMessageTemplateDTO;
import com.senlan.water.dto.sms.ShortMessageTemplateEditDTO;
import com.senlan.water.dto.sms.ShortMessageTemplateSearchDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 短信模板控制器
 */
@RestController
@RequestMapping("/sms")
@Slf4j
@SaCheckLogin
public class ShortMessageTemplateController {

    @Resource
    private ShortMessageTemplateService shortMessageTemplateService;

    /**
     * 新增短信模板
     * @param addDTO
     * @return
     */
    @PostMapping("add")
    public BaseResult<Boolean> add(@RequestBody ShortMessageTemplateAddDTO addDTO) {
        return BaseResult.data(shortMessageTemplateService.addNew(addDTO));
    }

    /**
     * 根据id编辑
     * @param editDTO
     * @return
     */
    @PostMapping("edit")
    public BaseResult<Boolean> edit(@RequestBody ShortMessageTemplateEditDTO editDTO){
        return BaseResult.data(shortMessageTemplateService.editById(editDTO));
    }

    /**
     * 启用
     * @param id
     * @return
     */
    @GetMapping("enable")
    public BaseResult<Boolean> enable(@RequestParam("id") Long id){
        return BaseResult.data(shortMessageTemplateService.enable(id));
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @GetMapping("disable")
    public BaseResult<Boolean> disable(@RequestParam("id") Long id){
        return BaseResult.data(shortMessageTemplateService.disable(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    public BaseResult<Boolean> delete(@RequestParam("id") Long id){
        return BaseResult.data(shortMessageTemplateService.deleteById(id));
    }

    /**
     * 分页查询
     * @param searchDTO
     * @return
     */
    @PostMapping("getPage")
    public BaseResult<Page<ShortMessageTemplateDTO>> getPage(@RequestBody ShortMessageTemplateSearchDTO searchDTO){
        Page<ShortMessageTemplate> page = shortMessageTemplateService.getPage(searchDTO);
        Page<ShortMessageTemplateDTO> pageDTO = new Page<>();
        BeanUtil.copyProperties(page,pageDTO);

        //把ShortMessageTemplate转成ShortMessageTemplateDTO
        if (CollectionUtil.isNotEmpty(page.getRecords())){
            List<ShortMessageTemplateDTO> list = new ArrayList<>();
            for (ShortMessageTemplate record : page.getRecords()){
                ShortMessageTemplateDTO shortMessageTemplateDTO = new ShortMessageTemplateDTO();
                BeanUtil.copyProperties(record,shortMessageTemplateDTO);
                shortMessageTemplateDTO.setPhoneList(Arrays.asList(record.getPhones().split(",")));
                list.add(shortMessageTemplateDTO);
            }
            pageDTO.setRecords(list);
        }
        return BaseResult.data(pageDTO);
    }
}
